package com.wf.controller;

import cn.hutool.json.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wf.domain.Menu;
import com.wf.domain.Role;
import com.wf.domain.RoleMenu;
import com.wf.domain.UserRole;
import com.wf.mapper.RoleMapper;
import com.wf.mapper.RoleMenuMapper;
import com.wf.service.RoleMenuService;
import com.wf.service.RoleService;
import com.wf.utils.LocationUtil;
import com.wf.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhiyu
 * @Date:18:22
 * @DESC:
 */
@RestController
@Api("权限管理控制层")
@CrossOrigin
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    RoleMenuService roleMenuService;
    @GetMapping("/allRole")
    @ApiOperation("根据id查询权限信息")
    public Result allRole(@RequestParam(required = false,value = "role_id") Integer role_id){
        //条件查询
        List<Role> list = roleService.selectRole(role_id);
        return Result.success(list);
    }
    @GetMapping("/selectRoleMenuByRoleId")
    @ApiOperation("根据roleid查询中间表信息")
    public Result selectRoleMenuByRoleId(@RequestParam(required = false,value = "role_id") Integer role_id){
        //条件查询
        List<Integer> list = roleMenuMapper.selectRoleMenuByRoleId(role_id);
        return Result.success(list);
    }
    @PutMapping("/insertRole")
    @ApiOperation("添加权限信息")
    public Result insertRole(@RequestBody Role role){
        int i = roleMapper.insert(role);
        boolean flag = false;
        if (i>0){
            flag = true;
        }
        return Result.success(flag);
    }
    @DeleteMapping("/deleteRoleById")
    @ApiOperation("根据id逻辑删除权限信息")
    @Transactional
    public Result deleteRole(@RequestParam(value = "roleId") Integer roleId){
        System.out.println("role_id = " + roleId);
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        int d = roleMenuMapper.delete(wrapper);
        int i = roleMapper.deleteById(roleId);
        boolean flag = false;
        if (i>0&&d>=0){
           flag = true;
        }
        return Result.success(flag);
    }
    @PostMapping("/editRole")
    @ApiOperation("根据id编辑权限信息")
    @Transactional
    public Result editRole(@RequestBody Map<String,Object> role){
        boolean b = roleMenuService.editRole(role);
        return Result.success(b);
    }
    @GetMapping("/addRole")
    @ApiOperation("根据id编辑权限信息")
    @Transactional
    public Result addRole(@RequestParam(value = "roleName") String roleName,@RequestParam(value = "remark") String remark){
//        System.out.println("roleName = " + roleName);
//        System.out.println("remark = " + remark);
        Role role = new Role();
        role.setRoleName(roleName);
        role.setRemark(remark);
        int insert = roleMapper.insert(role);
        return Result.success(insert>0);
    }
}
